package com.zz.ecommerce.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ZzStringUtil {

    public final static String TYPE_MARI = "Mari";//����
    public final static String TYPE_PORT = "Port";//�ۿ�
    public final static String TYPE_CIQ = "";//����


    public static String getFileSuffix(String fileName) {
        String suffix = "";
        if (StringUtils.isNotEmpty(fileName) && fileName.contains(".")) {
            suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        }
        return suffix;
    }


    public static String getFilePrefix(String fileName) {
        String suffix = fileName;
        if (StringUtils.isNotEmpty(fileName) && fileName.contains(".")) {
            suffix = fileName.substring(0, fileName.lastIndexOf("."));
        }
        return suffix;
    }


    public static String translateEndTime(String endTime) {
        if (StringUtils.isNotEmpty(endTime) && !endTime.contains(" ")) {
            endTime += " 23:59:59";
        }
        return endTime;
    }


    public static String translateStartTime(String startTime) {
        if (StringUtils.isNotEmpty(startTime) && !startTime.contains(" ")) {
            startTime += " 00:00:00";
        }
        return startTime;
    }


    public static String transLateUpper2UnderLine(final String name) {
        String rtn = name;
        if (StringUtils.isNotEmpty(name)) {
            rtn = name.replaceAll("([A-Z])", "_$1").toUpperCase();
        }
        return rtn;
    }


    public static String transLateUnderLine2Upper(final String name) {
        String rtn = "";
        if (StringUtils.isNotEmpty(name)) {
            String[] splits = name.toLowerCase().split("_");
            for (String str : splits) {
                rtn += (str.substring(0, 1).toUpperCase() + str.substring(1));
            }
            rtn = rtn.substring(0, 1).toLowerCase() + rtn.substring(1);
        }
        return rtn;
    }


    public static String getNowTime(String format) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        SimpleDateFormat dateFormat = new SimpleDateFormat(format, Locale.CHINA);
        return dateFormat.format(cal.getTime());
    }


    public static String capFirst(final String str) {
        String rtnStr = str;
        if (StringUtils.isNotEmpty(str)) {
            rtnStr = str.substring(0, 1).toUpperCase() + str.substring(1);
        }
        return rtnStr;
    }

    public static String generateRandomStr(int minLen, int maxLen) {
        int len = minLen + (int) Math.round(Math.random() * (maxLen - minLen));
        String generateSource = "0123456789";
        String rtnStr = "";
        for (int i = 0; i < len; i++) {
            rtnStr += generateSource.charAt((int) Math.floor(Math.random() * generateSource.length()));
        }
        return rtnStr;
    }


    public static void autoSetRandom(Object o) throws IllegalAccessException {
        Field fields[] = o.getClass().getDeclaredFields();
        StringBuffer fieilds = new StringBuffer("(");
        StringBuffer values = new StringBuffer("VALUES(");
        for (Field field : fields) {
            field.setAccessible(true);
            if (String.class == field.getType()) {
                field.set(o, generateRandomStr(3, 5));
            }
        }
    }


    public static String getSendTime() {
        return getNowTime("yyyyMMddHHmmss+08");
    }


    public static String getMsSendTime() {
        return getNowTime("yyyyMMddHHmmssSSS");
    }


    public static String getDefaultDateTime() {
        return getNowTime("yyyyMMddHHmmss");
    }



    public static Map<String, Boolean> mapEditAble = new HashMap<String, Boolean>() {
        {
            put("1", true);
            put("2", false);
            put("3", true);
            put("4", false);
            put("5", false);
            put("6", false);
            put("7", false);
            put("", false);
        }
    };


    public static String getTemplateClassName(Class clazz, String type) {
        String className = clazz.getSimpleName();
        if (StringUtils.isNotEmpty(type)) {
            className += ("#" + type);
        }
        return className;
    }


    public static void generateSizeFile(int size, String fileName) {
        byte[] bytes = new byte[size];
        File file = new File(fileName);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            FileOutputStream out = new FileOutputStream(file);
            out.write(bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static String getFindMsg(String ciqMsg, String regFind) {
        String findMsg = "";
        Pattern pattern;
        pattern = Pattern.compile(regFind);
        Matcher matcher = pattern.matcher(ciqMsg);
        if (matcher.find() && matcher.groupCount() > 0) {
            findMsg = matcher.group(1);
        }
        return findMsg.trim();
    }

    public static void main(String[] args) {
        System.out.println(getFindMsg(".*Ԥ¼���Ϊ�� 12345    6 AB", ".*Ԥ¼���Ϊ��\\s*([\\S]+).*"));
        System.out.println(getFindMsg(".*����ͨ�ص��ţ�    12323 ������", ".*����ͨ�ص��ţ�\\s*([\\S]+)[^��]*��.*"));
        System.out.println(ZzStringUtil.getFindMsg("ʩ�����:123123", "(.*)\\pP"));
    }
}
